package com.huichaoliu.adapters;

import com.huichaoliu.adapters.ProductAdapter.ViewHolder;
import com.huichaoliu.app.R;
import com.huichaoliu.data.Config;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CoolPhotoAdapter extends BaseAdapter {

	private Context mContext = null;
	private View layout_bottom = null;
	private EditText et_input = null;
	public CoolPhotoAdapter(Context mContext,View layout_bottom,EditText et_input){
		this.mContext = mContext;
		this.layout_bottom = layout_bottom;
		this.et_input = et_input;
	}
	
	public CoolPhotoAdapter(Context mContext){
		this.mContext = mContext;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return 11;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if(convertView == null){
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.item_cool_photos, null);
			holder = new ViewHolder();
			holder.init(convertView);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		final ViewHolder vh = holder;
		if(et_input == null){
			vh.layout_operate.setVisibility(View.GONE);
			vh.layout_do_th.setVisibility(View.GONE);
		}
		holder.ic_operate.setTag(null);
		holder.ic_operate.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(!Config.isOnline(mContext)){
					return;
				}
				if(v.getTag() == null){
					vh.layout_do_th.setVisibility(View.VISIBLE);
					v.setTag("");
				}else{
					vh.layout_do_th.setVisibility(View.GONE);
					v.setTag(null);
				}
			}
		});
		holder.ic_comment.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				layout_bottom.setVisibility(View.VISIBLE);
				layout_bottom.setTag(position + "");
				et_input.requestFocus();
				InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
				inputMethodManager.showSoftInput(et_input,InputMethodManager.SHOW_FORCED); 
			}
		});
		return convertView;
	}


	static class ViewHolder{
		private ImageView ic_cover;
		private ImageView ic_avatar;
		private TextView tv_name;
		private ImageView ic_like;
		private ImageView ic_comment;
		private ImageView ic_operate;
		private LinearLayout layout_comment;
		private LinearLayout layout_do_th;
		private View layout_operate;
		
		private void init(View view){
			ic_cover = (ImageView)view.findViewById(R.id.ic_cover);
			ic_avatar = (ImageView)view.findViewById(R.id.ic_avatar);
			tv_name = (TextView)view.findViewById(R.id.tv_name);
			ic_like = (ImageView)view.findViewById(R.id.ic_like);
			ic_comment = (ImageView)view.findViewById(R.id.ic_comment);
			ic_operate = (ImageView)view.findViewById(R.id.ic_comment_gold);
			layout_do_th = (LinearLayout)view.findViewById(R.id.layout_do_th);
			layout_operate = view.findViewById(R.id.layout_operate);
		}
	}
}
